﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.Base;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.Base
{
    /// <summary>
    /// 仓位管理
    /// </summary>
    public class StorageService:BasicService<Storage>
    {
        public StorageService(ISession session)
            : base(session)
        {

        }




        public bool CheckIsUsed(int storageId)
        {
            string sql = "select fullName from Ev_Storage where id=" + storageId;

            string fullName = this.mSession.CreateSQLQuery(sql).UniqueResult<string>();

            sql = "select count(*) from Ev_StorageEvidence as se inner join Ev_Storage as s on se.StorageId=s.Id where s.FullName ='"+fullName+"' or s.FullName like '" + fullName + ".%' ";

            int count=this.mSession.CreateSQLQuery(sql).UniqueResult<int>();

            return count==0?false:true;
        }

        public bool CheckIsLeaf(int storageId)
        {
            string sql = "select IsLeaf from Ev_Storage where Id=" + storageId;

            bool isLeaf = this.mSession.CreateSQLQuery(sql).UniqueResult<bool>();

            return isLeaf;
        }

        public override void SaveOrUpdate(Storage entity)
        {
            ITransaction tx = null;

            try
            {
                tx = mSession.BeginTransaction();

                bool isNew = entity.Id == 0 ? true : false;

                Storage parent = null;

                if (isNew)//新增
                {
                    parent = entity.Parent;
                    if (parent != null && parent.IsLeaf)
                    {
                        parent.IsLeaf = false;
                        base.SaveOrUpdate(parent);
                    }

                }
                base.SaveOrUpdate(entity);

                if (isNew)
                {
                    entity.FullPath = parent == null ? entity.Id.ToString() : parent.FullPath + "." + entity.Id.ToString();
                    entity.FullName = parent == null ? entity.Name : parent.FullName + "." + entity.Name;
                    base.SaveOrUpdate(entity);
                }
                tx.Commit();
            }
            catch (Exception ex)
            {
                if (tx != null)
                {
                    tx.Rollback();
                }
                throw ex;
            }
        }

        public void Delete(int storageId)
        {
            string sql = "select count(*) from Ev_Storage as s1 where s1.ParentId=(select ParentId from Ev_Storage s2 where s2.Id=" + storageId +")";

            int count = this.mSession.CreateSQLQuery(sql).UniqueResult<int>();

            if (count == 1)
            {
                this.mSession.CreateSQLQuery("update s1 set IsLeaf = 1 from Ev_Storage as s1 inner join Ev_Storage as s2 on s1.Id=s2.ParentId where s2.Id= " + storageId).ExecuteUpdate();

            }

            this.mSession.CreateQuery("delete Storage where Id="+storageId).ExecuteUpdate();

            
        }
    }
}
